/**
 * 商品备案申请表单js
 */

/**
 * 表单自定义控制器
 */
app.controller('btnCtrl', ['$scope', '$http', BtnController]);
function BtnController($scope, $http) {
	$scope.bindBtn = function(event) {
		switch(event){
			case 'event-to-add': // 新增
			{
				if ($scope.valueMap['commodityProperty'] == '优品') {
					var selectedData = [];

					$scope.showModalCheckBox('TP_ProductBasis_Yp','id',selectedData );
				}

				if ($scope.valueMap['commodityProperty'] == '新品') {
					var rowCurrent = {};
					rowCurrent.id = null;
					rowCurrent.code = '保存后自动生成';
					rowCurrent.name = '';
					rowCurrent.priceService = '0';
					rowCurrent.codeMerge = '保存后自动生成';
					rowCurrent.isSuperior = "否";
					rowCurrent.vsfJp = "否";
					rowCurrent.commodityProperty = $scope.valueMap['commodityProperty'];
					$scope.subGrid.sonlist.push(rowCurrent);
				}

				if ($scope.valueMap['commodityProperty'] == '变体') {
					$scope.showModalRadio('TP_ProductBasis_Bt',3, "code",59);
				}

				if ($scope.valueMap['commodityProperty'] == '精品') {
					var rowCurrent = {};
					rowCurrent.id = null;
					rowCurrent.code = '保存后自动生成';
					rowCurrent.name = '';
					rowCurrent.priceService = '0';
					rowCurrent.codeMerge = '保存后自动生成';
					rowCurrent.isSuperior = "否";
					rowCurrent.vsfJp = "是";
					rowCurrent.commodityProperty = $scope.valueMap['commodityProperty'];
					$scope.subGrid.sonlist.push(rowCurrent);
				}
				break;
			}
			case 'event-to-delete': // 删除
			{
				var datas = [];
				var datasDel = [];
				for (var i = 0; i < $scope.subGrid.sonlist.length; i++) {
					var data = $scope.subGrid.sonlist[i];
					if(data['ys_form_sonlist_checkbox'] == true) {
						if (data.id != null) {
							datasDel.push(data.id);
						}
						continue;
					} else {
						datas.push(data);
					}
				}
				if (datasDel.length != 0){
					$http.post('/api/inter/tpProductBq/del', {}, {params: {list: datasDel}})
						.success(function (response) {
								if(response.code == 1) {
									layer.closeAll();
									return layer.msg("删除失败，请稍后再试！", {icon: 2});
								}
							}
						);
				}

				$scope.subGrid.sonlist = datas;
				layer.msg("删除成功!", {icon: 2});
				break;
			}
		};
	}


	$scope.estimate = function() {
		layer.msg('该商品信息未保存,不允许编辑详情!', {icon: 2});
	}

}

/**
 * 子列表控制器
 */
app.controller('subGridCtrl', ['$scope', '$http', function SubGridController($scope, $http){
	//多选框回调
	$scope.checkbox.afterSelectDatas = function(rowsSelected) {
		for (var i = 0; i < rowsSelected.length; i++) {
			var obj = rowsSelected[i];
			var rowCurrent = {};
			rowCurrent.id = null;
			rowCurrent.code = '保存后自动生成';
			rowCurrent.name = '保存后自动生成'
			rowCurrent.priceService = 0
			rowCurrent.codeMerge = obj.codeMerge;
			rowCurrent.isSuperior = "是";
			rowCurrent.vsfJp = obj.vsfJp;
			rowCurrent.spid = obj.id
			rowCurrent.commodityProperty = '优品';
			$scope.subGrid.sonlist.push(rowCurrent);
		}
	}

}]);

/*
 * 自定义通用控制器，若无定义，页面会报错
 */
app.controller('formSelfCtrl', ['$scope', '$http', function FormSelfController($scope, $http){

	//单选回调
	$scope.doSelectRadioBySelf = function(data) {
		var rowCurrent = {};
		rowCurrent.id = null;
		rowCurrent.code = '保存后自动生成';
		rowCurrent.name = data.name
		rowCurrent.priceService = '0'
		rowCurrent.codeMerge = '保存后自动生成';
		rowCurrent.isSuperior = data.isSuperior;
		rowCurrent.vsfJp = data.vsfJp;
		rowCurrent.commodityProperty = '变体';
		$scope.subGrid.sonlist.push(rowCurrent);
	}
}

]);
